package nio.nio.zeroCopy;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author 帅小伙
 * @date 2022/1/15
 * @description
 * 服务器端
 */
public class NewIOServer {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(7001));

        // 创建一个buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while(true){
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readCount = 0;
            while(-1!=readCount){
                readCount = socketChannel.read(byteBuffer);

                // 倒带   position为0  mark = -1
                byteBuffer.rewind();
            }
        }
    }
}
